package org.login;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.common.utils.RandomUtils;
import org.login.obj.GameServerContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 游戏服管理器
 */
@Slf4j
@Component
public class GameServerManger {

    private final Map<Integer, GameServerContext> gameServerContextMap = new HashMap<>();

    public void registerGameServer(int serverId, Channel channel) {
        if (gameServerContextMap.containsKey(serverId)) {
            log.error("游戏服节点已经注册 serverId = {}", serverId);
            return;
        }
        gameServerContextMap.put(serverId, new GameServerContext(serverId, channel));
    }

    public int gameServerNum() {
        return gameServerContextMap.size();
    }

    public GameServerContext getGameServer(int serverId) {
        return gameServerContextMap.get(serverId);
    }

    /**
     * 随机一个游戏服节点
     */
    public GameServerContext randomGameServer() {
        return RandomUtils.selectOne(gameServerContextMap.values());
    }

}
